﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Tools.Utils;
using Tools.Web.Models;
namespace Tools.Web.Controllers
{
    public class CommonController : Controller
    {
       
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        public IActionResult UploadFile()
        {
            var formCollection = Request.Form;
            FormFileCollection filelist = (FormFileCollection)formCollection.Files ?? new FormFileCollection();
            ResultMessage resultMessage = new ResultMessage();
            string saveFilePath = $"{FileHelper.UploadFilePath}\\files\\temp\\";
            DirectoryInfo di = new DirectoryInfo(saveFilePath);
            if (!di.Exists) { di.Create(); }

            var filePathList = new List<string>();
            var fileNameList = new List<string>();
            var fileIconList = new List<string>();

            var path = "";
            var fileName = "";
            var fileIcon = "";
            var fileIconKeys = FileHelper.fileIconDictionary.Keys;
            try
            {
                foreach (IFormFile file in filelist)
                {
                    fileName = file.FileName;
                    string newFileName = Guid.NewGuid().ToString().Replace("-", "");
                    string suffix = System.IO.Path.GetExtension(fileName);
                    var saveFileItemPath = $"{saveFilePath}{newFileName}{suffix}";
                    path = $"/files/temp/{newFileName}{suffix}";
                    fileIcon = path;

                    if (fileIconKeys.Any(t => t.Contains(suffix.ToLower()))) 
                    {
                        var fileIconEntity = FileHelper.fileIconDictionary.First(t => t.Key.Contains(suffix.ToLower()));
                        fileIcon = fileIconEntity.Value;
                    }

                    using (var fs = System.IO.File.Create(saveFileItemPath))
                    {
                        file.CopyTo(fs);
                        fs.Flush();
                    }

                    filePathList.Add(path);
                    fileNameList.Add(fileName);
                    fileIconList.Add(fileIcon);
                }

                if (!filePathList.Any()) 
                {
                    return Json(resultMessage.Failure("请上传文件！如已上传，请刷新之后重试。"));
                }

                return Json(resultMessage.Success(new { filePath = filePathList, fileName = fileNameList, fileIcon= fileIconList }));
            }
            catch (Exception ex)
            {
                return Json(resultMessage.Failure("程序异常，请稍后重试。"));
            }
        }

        /// <summary>
        /// 下载API文档
        /// </summary>
        /// <param name="fileName">下载文件名称</param>
        /// <param name="suffix">后缀名称</param>
        /// <param name="folder">指定文件夹</param>
        /// <returns></returns>
        public IActionResult DownloadFile(string filePath)
        {
            filePath = $"{ FileHelper.UploadFilePath}/{filePath}";

            if (!System.IO.File.Exists(filePath))
            {
                return Content("下载文件不存在");
            }

            return FileHelper.FileRead(Path.GetFileName(filePath), new DirectoryInfo(filePath).Parent.FullName);
        }
    }
}
